package com.zhonglz.system.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zhonglz.commons.base.BaseController;
import com.zhonglz.model.SysMenu;
import com.zhonglz.util.result.ResultUtils;
import com.zhonglz.util.tools.ObjectUtils;
import com.zhonglz.util.tools.TreeList;
import com.zhonglz.util.tools.UuidUtils;


/***
 * MenuController 菜单接口
 * @author zhonglz saxon
 */
@RestController
@RequestMapping("/sys/menu/")
public class MenuController extends BaseController {

	/**
	 * 根据用户Id查询用户菜单
	 * @return
	 */
	@RequestMapping("getAllMenuByAll")
	public ResultUtils getAllMenuByAll() {
		List<SysMenu> list = sysMenuService.getAllMenuByAll();
		List<SysMenu> returnList = TreeList.getChildMenuList(list);
		return new ResultUtils(ResultUtils.CODE_SUCCESS,returnList);
	}
	
	
	/**
	 * 根据菜单类型获取菜单信息
	 * @return
	 */
	@RequestMapping("getMenuListByMenuType")
	public ResultUtils getMenuListByMenuType(Byte type) {
		if(ObjectUtils.isEmpty(type)) return new ResultUtils(ResultUtils.CODE_ERROR_NULL,"类型不能为空");
		HashMap<String, Object> params = new HashMap<String, Object>(); 
		params.put("menuType", type);
		List<SysMenu> list = sysMenuService.getMenuListByParams(params);
		return new ResultUtils(ResultUtils.CODE_SUCCESS,list);
	}

	
	// 更新菜单
	@ResponseBody
	@RequestMapping("updateMenu")
	public ResultUtils updateMenu(SysMenu menu,HttpServletRequest req){
		try {
			boolean flag = sysMenuService.updateMenu(menu);
			if(flag){
				return new ResultUtils(ResultUtils.CODE_SUCCESS, "更新菜单成功");
			}else{
				return new ResultUtils(ResultUtils.CODE_ERROR, "更新菜单失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultUtils(ResultUtils.CODE_EXCEPTION, "更新菜单发生异常");
		}
	}
	
	// 删除菜单
	@ResponseBody
	@RequestMapping("deleteMenu")
	public ResultUtils deleteMenu(String menuId,HttpServletRequest req){
		try {
			boolean flag = sysMenuService.deleteMenu(menuId);
			if(flag){
				return new ResultUtils(ResultUtils.CODE_SUCCESS, "删除菜单成功");
			}else{
				return new ResultUtils(ResultUtils.CODE_ERROR, "删除菜单失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultUtils(ResultUtils.CODE_EXCEPTION, "删除菜单发生异常");
		}
	}
	
	// 插入菜单
    @ResponseBody
    @RequestMapping("insertMenu")
    public ResultUtils insertMenu(SysMenu menu){
        menu.setId(UuidUtils.uuid());
        if(ObjectUtils.isNotEmpty(menu.getMenuParentNo())){
            menu.setMenuNo(sysMenuService.createMenuNo(menu.getMenuParentNo()));
            menu.setMenuParentNos(sysMenuService.createMenuParentNos(menu.getMenuParentNo(),menu.getMenuNo()));
        }else{
            menu.setMenuNo(sysMenuService.createMenuNo(null));
        }

        if(ObjectUtils.isEmpty(menu.getMenuIcon())){
            if(menu.getMenuType() == 1){
                menu.setMenuIcon("el-icon-folder");
            }else if(menu.getMenuType() == 2){
                menu.setMenuIcon("el-icon-document");
            }

        }
        menu.setCreateTime(new Date());
        boolean flag = sysMenuService.insertMenu(menu);
        if(flag){
            return new ResultUtils(ResultUtils.CODE_SUCCESS, "添加菜单成功");
        }else{
            return new ResultUtils(ResultUtils.CODE_ERROR, "添加菜单失败");
        }
    }
}
